package com.sjz.han.industry.activites;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjz.han.industry.R;
import com.sjz.han.industry.base.BaseActivity;
import com.sjz.han.industry.entities.LoignEntity;
import com.sjz.han.industry.entities.PersonEntity;
import com.sjz.han.industry.internetmanage.ReqestUrl;
import com.sjz.han.industry.utils.JsonUtils;
import com.sjz.han.industry.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

//个人资料编辑界面
public class PersonMsgEditActivity extends BaseActivity implements View.OnClickListener{

    private EditText msg_name_edt,msg_phone_edt,msg_bankname_edt,msg_bankcode_edt,msg_adress_edt,msg_otherphone_edt;
    private String name="",phone="",bankname="",bankcode="",adress="",other_phone="";
    private Button msg_upload_btn;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_personmsgedit;
    }

    @Override
    protected void initDatas() {
      getMsg();
    }

    @Override
    protected void initViews() {
        reback_btn=(RelativeLayout)this.findViewById(R.id.reback_btn);
        titleTv=(TextView)this.findViewById(R.id.ctitle_tv);
        titleTv.setText("编辑个人资料");

        msg_name_edt=(EditText)this.findViewById(R.id.msg_name_edt);
        msg_phone_edt=(EditText)this.findViewById(R.id.msg_phone_edt);
        msg_bankname_edt=(EditText)this.findViewById(R.id.msg_bankname_edt);
        msg_bankcode_edt=(EditText)this.findViewById(R.id.msg_bankcode_edt);
        msg_adress_edt=(EditText)this.findViewById(R.id.msg_adress_edt);
        msg_otherphone_edt=(EditText)this.findViewById(R.id.msg_otherphone_edt);

        msg_upload_btn=(Button)this.findViewById(R.id.msg_upload_btn);
    }

    @Override
    protected void setLisener() {
        reback_btn.setOnClickListener(this);
        msg_upload_btn.setOnClickListener(this);
    }

    @Override
    protected void onRetry() {

    }

    @Override
    public void onClick(View v) {
        name=msg_name_edt.getText().toString().trim();
        phone=msg_phone_edt.getText().toString().trim();
        bankname=msg_bankname_edt.getText().toString().trim();
        bankcode=msg_bankcode_edt.getText().toString().trim();
        adress=msg_adress_edt.getText().toString().trim();
        other_phone=msg_otherphone_edt.getText().toString().trim();
        switch (v.getId()){
            case R.id.reback_btn:
                finish();
                break;
            case R.id.msg_upload_btn:
                if(TextUtils.isEmpty(name)){
                    ToastUtils.popUpToast("姓名不可为空！");
                }else if(TextUtils.isEmpty(phone)){
                    ToastUtils.popUpToast("手机号不可为空！");
                }else if(TextUtils.isEmpty(other_phone)){
                    ToastUtils.popUpToast("其他联系方式不可为空！");
                }else if(TextUtils.isEmpty(bankname)){
                    ToastUtils.popUpToast("银行名称不可为空！");
                }else if(TextUtils.isEmpty(bankcode)){
                    ToastUtils.popUpToast("银行卡号不可为空！");
                }else if(TextUtils.isEmpty(adress)){
                    ToastUtils.popUpToast("地址不可为空！");
                }else{
                    alterMsg();
                }
                break;
        }
    }

    public void alterMsg(){
        showLoadingDialog("正在提交...");
        OkHttpUtils
                .post()
                .url(ReqestUrl.PERSONMSG_EDT_URL)
                .addParams("token",shareUtils.getToken())
                .addParams("username",name)
                .addParams("phone",phone)
                .addParams("bankName",bankname)
                .addParams("bankCode",bankcode)
                .addParams("address",adress)
                .addParams("otherPhone",other_phone)
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    LoignEntity entity= JsonUtils.getObject(response,LoignEntity.class);
                    if(entity!=null&&entity.code==0){
                        ToastUtils.popUpToast(entity.msg);
                        finish();

                    }else{
                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }

    public void getMsg(){
        showLoadingDialog("正在获取数据...");
        OkHttpUtils
                .post()
                .url(ReqestUrl.PERSONMSG_URL)
                .addParams("token",shareUtils.getToken())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    PersonEntity entity= JsonUtils.getObject(response,PersonEntity.class);
                    if(entity!=null&&entity.code==0){
                      if(entity.data!=null){
                          setPersonMsg(entity.data);
                      }
                    }else{
                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }
    public void setPersonMsg(PersonEntity.PersonData entity){
        msg_name_edt.setText(entity.username);
        msg_phone_edt.setText(entity.phone);
        msg_bankname_edt.setText(entity.bankName);
        msg_bankcode_edt.setText(entity.bankCode);
        msg_adress_edt.setText(entity.address);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }
}
